package com.rose.redis._bloom_filter;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

/**
 * <p>
 * 布隆过滤器
 * 参考：https://github.com/redisson/redisson/wiki/6.-%E5%88%86%E5%B8%83%E5%BC%8F%E5%AF%B9%E8%B1%A1
 * </p>
 *
 * @author: zhu.chen
 * @date: 2020/12/31
 * @version: v1.0.0
 */
//@Component
public class RoseBloomFilter implements CommandLineRunner {

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public void run(String... args) throws Exception {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter("sample");
        // 初始化布隆过滤器，预计统计元素数量为55000000，期望误差率为0.03
        bloomFilter.tryInit(55000000L, 0.03);
        bloomFilter.add("1");
        bloomFilter.add("2");
        System.out.println(bloomFilter.contains("2"));
    }

}
